package com.yociyy.gen.controller;

import com.github.pagehelper.PageHelper;
import com.yociyy.common.core.msg.TableR;
import com.yociyy.gen.biz.GenTableBiz;
import com.yociyy.gen.config.GenConfig;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 代码生成器
 *
 * @author: YoCiyy
 * @date: 2020/4/15
 */
@RequestMapping("/gen")
@RestController
public class GenController {

	@Autowired
	private GenTableBiz genTableBiz;

	@GetMapping("/page")
	public TableR page(@RequestParam Map<String, Object> params) {
		System.out.println(GenConfig.getAuthor());
		PageHelper.startPage(params);
		return new TableR(genTableBiz.selectDBTableList(params));
	}

	@GetMapping("/code")
	public void gen(@RequestParam String tables, HttpServletResponse response) throws IOException {
		String[] tableNames = tables.split(",");
		byte[] data = genTableBiz.genCode(tableNames);
		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=ag-admin-code.zip");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IOUtils.write(data, response.getOutputStream());
	}
}
